/*
 * Created on 2004-01-06
 */
package org.opencube.oms;

import java.util.Date;

/**
 * @author <a href="mailto:maciek@fingo.pl">FINGO - Maciej Mroczko</a>
 * TODO : comment 
 */
class Value extends OMSNode
{
	/* The datatype enumeration for this value */
	private String datatypeEnumeration = null;
	/* The simple attribute which value defines this object */
	private String simpleAttribute = null;
	/* The simple value owner-element */
	private OMSElement element = null;
	/* The content of this value*/
	private Object content = null;

	/**
	 * Full constructor.
	 * 
	 * @param datatypeEnumeration
	 * @param simpleAttribute
	 * @param id
	 * @param content
	 * @param creatingDate
	 * @param creater
	 * @param modifyingDate
	 * @param modifier
	 */
	Value( OMSElement element, String datatypeEnumeration, String simpleAttribute, String id, Object content, Date creatingDate, String creater, Date modifyingDate, String modifier )
	{
		super( id, null, creatingDate, creater, modifyingDate, modifier );
		this.element = element;
		this.simpleAttribute = simpleAttribute;
		this.datatypeEnumeration = datatypeEnumeration;
		this.content = content;
	}

	/**
	 * Returns the datatype enumeration, the value is associated with
	 * 
	 * @return DatatypeEnumeration - the datatype enumeration
	 */
	public String getDatatypeEnumeration()
	{
		return this.datatypeEnumeration;
	}

	/**
	 * Returns the simple attribute, the value is associated with
	 * 
	 * @return SimpleAttribute - simple attribute
	 */
	String getSimpleAttribute()
	{
		return this.simpleAttribute;
	}
	
	/**
	 * Returns the content as String.
	 * 
	 * @return The content value as String
	 * 
	 * @throws OMSMetaDataException when datatype of this value is a blob type 
	 */
	String getContentAsString()
	{
		return (String) this.content;
	}
	
	/**
	 * Returns the content as String.
	 * 
	 * @return The value content
	 */
	Object getContent()
	{
		return this.content;
	}
	
	/**
	 * Returns the content as byte array.
	 * 
	 * @return The value content as byte array
	 * 
	 * @throws OMSMetaDataException when datatype of this value is not a blob type 
	 */
	byte[] getContentAsByteArray()
	{
		return (byte[]) this.content;
	}

	/**
	 * Returns the owner element
	 * 
	 * @return Element - the owner element
	 */
	OMSElement getElement()
	{
		return this.element;
	}
	
	/**
	 * Sets the value content
	 * 
	 * @param content the new value content
	 */
	void setContent( Object content )
	{
		if( content instanceof String && content != null && ( (String) content ).trim().length() == 0 )
		{
			content = null;
		}
		if( !( content == null && this.content == null ) && !( content != null && content.equals( this.content ) ) )
		{
			this.content = content;
			if( !isNew() )
			{
				setState( STATE_UPDATED );
			}
		}
	}
}
